﻿Imports System.Text
Imports CRM.BusinessLayer
Partial Public Class frmAddCompanyLink
    Public Enum LinkMode
        NotSet = 0
        Subsidiary = 1
        Parent = 2
    End Enum
    Private _ScreenMode As LinkMode

    Public Property ScreenMode() As LinkMode
        Get
            Return _ScreenMode
        End Get
        Set(ByVal value As LinkMode)
            _ScreenMode = value

            Select Case _ScreenMode
                Case LinkMode.Parent
                    Me.grpLink.Header = "Add Parent Company"
                Case LinkMode.Subsidiary
                    Me.grpLink.Header = "Add Subsidiary Company"
            End Select
        End Set
    End Property
    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnFind.Click
        If IsNumeric(Me.txtCompanyId.Text) Then
            DataBinding.BindListView(Company.SearchCompany(CLng(Me.txtCompanyId.Text)), Me.lvwCompany)
        ElseIf Me.txtCompanyName.Text.Trim <> "" Then
            DataBinding.BindListView(Company.SearchCompany(Me.txtCompanyName.Text), Me.lvwCompany)
        Else
            CommonFuncs.DisplayMessage("Please enter a Company ID or Company Name.", CommonDialog.CustomDialogIcons.Information, "Company")
        End If
    End Sub

    Private Sub btnAddLink_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddLink.Click
        Dim tsk As New TasksEvents
        Try

        
            If Me.lvwCompany.SelectedItems.Count = 0 Then
                Exit Sub
            End If

            If Me.lvwCompany.SelectedItem.row.itemarray(0).ToString = GlobalCRM.CurrentCompanyID Then
                CommonFuncs.DisplayMessage("Cannot set a company to be a subsidiary of itself!", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            Dim Msg1 As New StringBuilder
            Dim Msg2 As New StringBuilder
            Dim Msg3 As New StringBuilder
            Dim ParentCoId As Long = Me.lvwCompany.SelectedItem.row.itemarray(0).ToString


            If Me._ScreenMode = LinkMode.Parent Then

                Dim OldParentCompany As Company.CompanyInfo = Company.GetParentCompany(GlobalCRM.CurrentCompanyID)

                If OldParentCompany.CompanyID = 0 Then
                    Msg1.Append(Me.lvwCompany.SelectedItem.row.itemarray(1).ToString & " (" & Me.lvwCompany.SelectedItem.row.itemarray(0).ToString & ") added as parent company.")
                    Msg2.Append(GlobalCRM.CurrentCompanyName & " (" & GlobalCRM.CurrentCompanyID & ") Set as subsidiary.")
                Else
                    If MsgBox("Change parent from " & GlobalCRM.CurrentCompanyName & " to " & Me.lvwCompany.SelectedItem.row.itemarray(1).ToString & "?", vbYesNo, "Confirm change") <> MsgBoxResult.Yes Then
                        Exit Sub

                    End If

                    Msg1.Append("Parent company changed from " & GlobalCRM.CurrentCompanyName & " to " & Me.lvwCompany.SelectedItem.row.itemarray(1).ToString & " (" & GlobalCRM.CurrentCompanyID & ")")
                    Msg2.Append(GlobalCRM.CurrentCompanyName & " (" & GlobalCRM.CurrentCompanyID & ") Set as subsidiary.")
                End If

                If Not Company.UpdateParent(GlobalCRM.CurrentCompanyID, Me.lvwCompany.SelectedItem.row.itemarray(0).ToString) Then
                    Exit Sub
                End If

                tsk.GblFunc_Add_Event("Details Changed", Msg1.ToString, 1, GlobalCRM.CurrentCompanyID, tasksevents.EventTypes.EventCompany)
                tsk.GblFunc_Add_Event("Details Changed", Msg2.ToString, 1, Me.lvwCompany.SelectedItem.row.itemarray(0).ToString, TasksEvents.EventTypes.EventCompany)



            Else
                If ParentCoId <> 0 Then
                    'Already has a parent, get details and confirm

                    Dim myComp As Company.CompanyInfo = Company.GetCompany(ParentCoId)


                    If IsNothing(myComp) Then
                        CommonFuncs.DisplayMessage("Existing parent not found", CommonDialog.CustomDialogIcons.Information, "Error")
                        Exit Sub
                    End If

                    If MsgBox(GlobalCRM.CurrentCompanyName & " already has '" & myComp.CompanyName & "' as a parent, are you sure you want to change it to " & GlobalCRM.CurrentCompanyName & "?", vbYesNo, "Already has a parent") <> MsgBoxResult.Yes Then
                        Exit Sub
                    End If

                    Msg1.Append("Parent changed from '" & myComp.CompanyName & "' (" & myComp.CompanyID & ") to '" & GlobalCRM.CurrentCompanyName & "' (" & GlobalCRM.CurrentCompanyName & ")")
                    Msg2.Append(myComp.CompanyName & " (" & myComp.CompanyName & ") set as subsidiary.")
                    Msg3.Append("Subsidiary company '" & myComp.CompanyName & "' moved to be a subsidiary of '" & GlobalCRM.CurrentCompanyName & "' (" & GlobalCRM.CurrentCompanyID & ") instead")
                Else
                    Msg1.Append("Parent company set to '" & GlobalCRM.CurrentCompanyName & "' (" & GlobalCRM.CurrentCompanyID & ")")
                    Msg2.Append(Me.lvwCompany.SelectedItem.row.itemarray(1).ToString & " (" & Me.lvwCompany.SelectedItem.row.itemarray(0).ToString & ") set as subsidiary.")

                End If

                If Not Company.UpdateParent(Me.lvwCompany.SelectedItem.row.itemarray(0).ToString, GlobalCRM.CurrentCompanyID) Then
                    Exit Sub
                End If


                tsk.GblFunc_Add_Event("Details Changed", Msg1.ToString, 1, Me.lvwCompany.SelectedItem.row.itemarray(0).ToString, TasksEvents.EventTypes.EventCompany)
                tsk.GblFunc_Add_Event("Details Changed", Msg2.ToString, 1, GlobalCRM.CurrentCompanyID, tasksevents.EventTypes.EventCompany)
                If Msg3.ToString.Trim <> "" Then
                    tsk.GblFunc_Add_Event("Details Changed", Msg3.ToString, 1, ParentCoId, tasksevents.EventTypes.EventCompany)
                End If
            End If

            Me.Close()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAddCompanyLink : frmAddlink_click")
        Finally
            tsk = Nothing
        End Try
    End Sub

    Private Sub btnAddLink_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddLink.Loaded
        If Me.ScreenMode = LinkMode.NotSet Then
            CommonFuncs.DisplayMessage("Mode not set, contact developer!", CommonDialog.CustomDialogIcons.Information, "Mode")
            Me.Close()
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub frmAddCompanyLink_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
    End Sub
End Class
